<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form">
    <div class="mainBox  layui-card-body" style="padding: 0 0;">
        <div class="main-container">
            <div class="layui-tab" lay-filter="TabBrief">
                {if condition="$category_id eq 10015"}
                <ul class="layui-tab-title">
                    <li class="layui-this">直播课</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <div id="zhibo"></div>
                    </div>
                </div>
                {else/}
                <ul class="layui-tab-title">
                    <li class="layui-this">SPARK</li>
                    <li>轻学堂</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <div id="spark"></div>
                    </div>
                    <div class="layui-tab-item">
                        <div id="qxt"></div>
                    </div>
                </div>
                {/if}
            </div>
        </div>
    </div>


    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['form', 'jquery', 'element', 'transfer'], function() {
        let form = layui.form;
        let $ = layui.jquery;
        let element = layui.element;
        let transfer = layui.transfer;

        let spark = {$spark|raw};
        let spark_str = {$spark_str|raw};
        let qxt = {$qxt|raw};
        let qxt_str = {$qxt_str|raw};
        let zhibo = {$zhibo|raw};
        let zhibo_str = {$zhibo_str|raw};

        //显示搜索框
        transfer.render({
            elem: '#spark'
            ,data: spark
            ,width: 350
            ,height: 450
            ,text: {
                none: '无数据' //没有数据时的文案
                ,searchNone: '无匹配数据' //搜索无匹配数据时的文案
            }
            ,title: ['SPARK', '选中的SPARK']
            ,showSearch: true
            ,value: spark_str
            ,id: 'spark'
        });

        transfer.render({
            elem: '#qxt'
            ,data: qxt
            ,width: 350
            ,height: 450
            ,text: {
                none: '无数据' //没有数据时的文案
                ,searchNone: '无匹配数据' //搜索无匹配数据时的文案
            }
            ,title: ['轻学堂', '开放的轻学堂']
            ,showSearch: true
            ,value: qxt_str
            ,id: 'qxt'
        });


        transfer.render({
            elem: '#zhibo'
            ,data: zhibo
            ,width: 350
            ,height: 450
            ,text: {
                none: '无数据' //没有数据时的文案
                ,searchNone: '无匹配数据' //搜索无匹配数据时的文案
            }
            ,title: ['直播课', '开放的直播课']
            ,showSearch: true
            ,value: zhibo_str
            ,id: 'zhibo'
        });

        let tabid = 0;
        element.on('tab(TabBrief)', function(data){
            tabid = data.index;
        });


        form.on('submit(user-save)', function(data) {

            if ({$category_id} == 10015) {
                var data = transfer.getData('zhibo');
                var type = 'zhibo';
                if (data == '') {
                    var courseid = [];
                } else {
                    var courseid = [];
                    for ( var i = 0; i < data.length; i++){
                        courseid.push(data[i].value);
                    }
                }

                layer.confirm('确定保存吗?', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "/Category/saveCourse",
                        data:{category_id:'{$category_id}', type:type, courseid:courseid},
                        dataType: 'json',
                        type: 'post',
                        success: function(result) {
                            layer.close(loading);
                            if (result.status == 0) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
                return false;
            } else {
                if (tabid == 0) {

                    var datas = transfer.getData('spark');
                    var type = 'spark';

                } else if(tabid == 1) {

                    var datas = transfer.getData('qxt');
                    var type = 'qxt';

                }

                if (datas == '') {
                    var courseid = [];
                } else {
                    var courseid = [];
                    for ( var i = 0; i < datas.length; i++){
                        courseid.push(datas[i].value);
                    }
                }

                layer.confirm('确定保存吗?', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "/Category/saveCourse",
                        data:{category_id:'{$category_id}', type:type, courseid:courseid},
                        dataType: 'json',
                        type: 'post',
                        success: function(result) {
                            layer.close(loading);
                            if (result.status == 0) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
                return false;
            }
        });
    })
</script>
<script>
</script>
</body>
</html>